import Mock from 'mockjs'
import store from '../store'

const Success=(message)=>{
    return Mock.mock({
        code:200,
        status:true,
        message:message,
      })
}
const Error=(message)=>{
    return Mock.mock({
        code:500,
        status:false,
        message:message,
      })
}
Mock.mock('/api/getlist2', 'get', {
    status: 200,
    message: '获取商品列表成功',
    'data|5-10': [{ // 5-10随机生成5~10个数据
      'id|+1': 0,
      name: '@cword(2,3)', // 生成中文汉字，word生成英文汉字
      price: 10,
      count: 200,
      img: 'https://raw.githubusercontent.com/moon-ice/Typora-source/master/typora202005/15/163649-643563.png'
    }]
  })

Mock.mock('/api/login', 'post', (option)=> {
    // 这里的option是请求的相关参数
   
    let opUser=JSON.parse(option.body);
    let userList= store.state.user.userList;
    if (userList.length == 0) {
        return Error("用户不存在!");
      }
      let user = userList.find((item) => {
        return item.username == opUser.username;
      });

      if (user == undefined) {
        return Error("用户不存在!");
      }
      if (user.userpwd != opUser.userpwd) {
        return Error("密码错误!");
      }
     
   return Success('登陆成功!')
  
    
  })

  
 
  